<?
//Data 0.1.11
class data{
	//Defend from sql-injection
	public static function safe($text, $len=0){
		if($len) $text=substr($text, 0, $len);
		//$text=mysql_real_escape_string($text);
		$text=stripslashes($text);
		$test=str_replace("\"", "&quot;", $text);
		$test=str_replace("'", "&#39;", $text);
		return $text;
	}
	// strip slahes and trim the text
	public static function safe_sl($str)
	{
		return stripslashes(trim($str));
	}
	// strip slahes and trim the text
	public static function safe_html($str)
	{
		return strip_tags(stripslashes(trim($str)));
	}
	//Show HTML tags
	public static function html_view($text, $len=0){
		if($len) $text=substr($text, 0, $len);
		$text=htmlspecialchars(stripslashes($text));
		//$text=mysql_real_escape_string($text);
		return $text;
	}
	//Delete html tags
	public static function html_remove($text, $len=0, $symbols=' '){
		$text=strip_tags($text);
		//$text=mysql_real_escape_string($text);
		$nbsp=strpos($text, $symbols, $len);
		if($len) $text=substr($text, 0, $nbsp)+'...';
		return $text;
	}
	//Text to word
	public static function word($text, $language='en', $len=0, $addation=''){
		if($len) $text=substr($text, 0, $len);
		$text=str_replace(" ", "_", $text);
		$rep="";
		if(is_array($language)){
			foreach($language as $val){
				if($val=="ru"){
					$rep.="А-я";
				}
				elseif($val=="en"){
					$rep.="A-z";
				}
			}
		}
		else{
			if($language=="ru"){
				$rep.="А-я";
			}
			elseif($language=="en"){
				$rep.="A-z";
			}
		}
		$text=preg_replace("/[^".$addation.$rep."0-9_\-]/", "", $text);
		return $text;
	}
	//Only file name
	public static function file_name($text, $len=0){
		if($len) $text=substr($text, 0, $len);
		$text=str_replace(" ", "_", $text);
		$text=preg_replace("/[^A-z0-9_.-]/", "", $text);
		return $text;
	}
	//Free file
	public static function file_free($name, $dir){
		$right_name=data::file_name_without_type($name);
		$type=data::file_type($name);
		if(file_exists($dir.$right_name.".".$type)){
			$i=0;
			while(file_exists($dir.$right_name."_".$i.".".$type)){
				$i++;
			}
			$right_name.="_".$i;
		}
		return $dir.$right_name.".".$type;
	}
	//Filename without type
	public static function file_name_without_type($text, $len=0){
		$text=data::file_name($text, $len);
		$point=strrpos($text, ".");
		$text=substr($text, 0, $point);
		return $text;
	}
	//File system
	public static function file_system($text, $len=0){
		if($len) $text=substr($text, 0, $len);
		$text=str_replace(" ", "_", $text);
		$text=preg_replace("/[^A-z0-9_.-\/]/", "", $text);
		return $text;
	}
	//Only e-mail
	public static function email($text, $len=0){
		if($len) $text=substr($text, 0, $len);
		$text=preg_replace("/[^A-z0-9|_|@|.|-]/", "", $text);
		if(substr_count($text, "@")==1)	return $text;
		return 0;
	}
	//Only numbers
	public static function number($numb){
		$numb=str_replace(",", ".", $numb);
		$numb=preg_replace("/[^0-9|.]/", "", $numb);
		if(substr_count($numb, ".")>1)	return "";
		return $numb;
	}
	//Only list of numbers
	public static function phones($numb){
		$numb=preg_replace("/[^0-9. ,-+]/", "", $numb);
		return $numb;
	}
	//Html validation
	public static function valid($text){
		$text=str_ireplace("<br>", "<br />", $text);
		$text=str_ireplace("<hr>", "<hr />", $text);
		return $text;
	}
	//Is bool
	public static function is_bool($text){
		if(is_bool($text)) return true;
		if($text=="true"||$text=="false") return true;
		return false;
	}
	//Is int
	public static function is_int($text){
		if(is_int($text)) return true;
		if($text=='0') return true;
		if($text=='') return false;
		if($text[0]!="0"&&!preg_match("/[^0-9]/", $text)) return true;
		return false;
	}
	//Random string
	public static function rand($len){
		$a=array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
			 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
			 'u', 'v', 'w', 'x', 'y', 'z');
		$s="";
		$c=count($a)-1;
		for($i=0; $i<$len; $i++){
			$s.=$a[rand(0, $c)];
		}
		return $s;
	}
	//Add file postfix
	public static function add_file_postfix($file_name, $postfix)
	{
		$v = strrpos($file_name, '.');
		$name = substr($file_name, 0, $v).$postfix.substr($file_name, $v);
		return $name;
	}
	//Create unical name
	public static function get_uniq_name($name)
	{
		$uniq 			= $this->uniq(12);
		$ext 			= strtolower(substr($name,strrpos($name,"."),strlen($name))); // RASSHIRENIE
		return $uniq.$ext;
	}
	//Generete unical random name
	public static function uniq($n)
	{
		return substr(uniqid(md5(microtime())),0,$n);
	}
	//Return the file type
	public static function file_type($file_name){
		$dot=strrpos($file_name, ".");
		return strtolower(substr($file_name, $dot+1));
	}
	//Return the name of symbiont
	public static function symbiont($text){
		$text=preg_replace("/[^a-zA-Z0-9_\-\.\*]/", "", $text);
		return $text;
	}
	//Valid E-mail
	public static function valid_email($mail_addr)
	{
		return ereg("[0-9a-zA-Z_.-]*.@[0-9a-z_-]*.\.([a-z]){2,3}",$mail_addr);
	}
	//Remove all files in given folder and also the folder(recursive).
	public static function folder_delete($str)
	{
		if(is_file($str))
			{
		    return @unlink($str);
		}
		elseif(is_dir($str))
			{
		    $scan = glob(rtrim($str,'/').'/*');
		    foreach($scan as $index=>$path)
				{
			data::folder_delete($path);
		    }
		    return @rmdir($str);
		}
	}
	static function is_email($email){
		if(preg_match("/[0-9a-z_]+@[0-9a-z_^\.]+\.[a-z]{2,3}/i", $email)){
			return true;
		}
		return false;
	}
	static function url_exists($url){
		$r=true;
		preg_match("/http:\/\/([^\/]*)(.*)/", $url, $matches);
		$site=$matches[1];
		$page=$matches[2];
		$sock = fsockopen($site, 80, $en, $es, 10);
		if (!is_resource($sock)) $r=false;
		fwrite($sock, "HEAD $page HTTP/1.0\r\n\r\n");
		$content=fgets($sock, 256);
		if(preg_match("/.*(403|404).*/", $content)){
			$r=false;
		}
		fclose($sock);
		return $r;
	}
}
?>